package top.yxqz.springmvc.annotation.util;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: 余小小
 * @date: 2023/5/16
 * @description： 实现一个拦截器
 */
@Component
public class MyInterceptor implements HandlerInterceptor {

    /**
     * 进入controller之前会执行的方法
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("MyInterceptor--------------------------preHandle");
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }

    /**
     * controller执行完毕之后执行的方法
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("MyInterceptor--------------------------postHandle");
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    /**
     * postHandle执行完毕后执行
     * 执行的顺序是拦截器的倒叙（多个拦截器）
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("MyInterceptor--------------------------afterCompletion");
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
